<?
if(!$CFG) require_once('config.php');

if(key_exists('debug',$_GET) && $_GET['debug']) $debug = true;
else $debug = false;

if(key_exists('type',$_GET)) $type = $_GET['type'];
else $type = false;

function register_modules()
{
	global $CFG;

	$dir = $CFG->GownFull_MODULEPath;
	if(is_dir($dir)) {
		if($dh = opendir($dir)) {
			while(($file = readdir($dh)) !== false) {
				if(filetype($dir . $file) !== 'file') continue;
				if(ereg('^(.+(InputMethod)|.+)\.js$',$file,$reg)) {
					if($reg[2] !== 'InputMethod') {// non Input Method, we can go...
						printf("GownFull.instance.modman.RegisterDownload(\"%s\");\r\n",$reg[1]);
					}
				}
			}
			closedir($dh);
		}
	}
}

// FIXME: I remember that adding charset=utf8 to Content-Type
// will make IE using wrong default charset in Javascript.
// someone please check.
header('Content-Type: application/x-javascript');

if($debug) $file = $CFG->GownFull_javascript_debug_file;
else $file = $CFG->GownFull_javascript_file;

if($debug) {
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
	header('Cache-Control: no-cache, must-revalidate');
	header('Pragma: no-cache');
}
else {
	if(key_exists('HTTP_IF_MODIFIED_SINCE',$_SERVER)) {
		$ims = ereg_replace(';.*$', '',$_SERVER['HTTP_IF_MODIFIED_SINCE']);
		$ims = strtotime($ims);
	}
	else $ims = 0;
	$mtime = filemtime($file);
	if($ims >= $mtime) {
		header('HTTP/1.1 304 Not Modified');
		exit(0);
	}
	header('Cache-Control: max-age='.$CFG->http_cache_max_age);
	header('Last-Modified: '.gmdate('D, d M Y H:i:s',$mtime).' GMT');
}

readfile($file);

// check language

if(key_exists('lang',$_GET)) {
	$path = $CFG->GownFull_LANGPath.$_GET['lang'].'.js';
	if(file_exists($path)) {
		readfile($path);
	}
	else { // this language does not exists
		printf("\n//Language \"%s\" does not exist.\n",$_GET['lang']);
	}
}

// change the config
printf("GownFullConfig.gownfull_base_url = '%s';\r\n",$CFG->GownFull_URL);
printf("GownFullConfig.css_file = '%s';\r\n",$CFG->GownFull_URL . 'gownfull.css');
printf("GownFullConfig.getmod_url = '%s';\r\n",$CFG->GownFull_URL . 'getmod.php');
printf("GownFullConfig.help_url = '%s';\r\n",$CFG->GownFull_URL . 'help/');
printf("GownFullConfig.default_output_code = ':assocword:googlesuggest:output';\r\n");

if($type == 'frame') {
	printf("new GownFull(new FramesetGownFullBuilder);\r\n");

	if(!$_GET['noregdownload']) {
		register_modules();
		foreach($CFG->Available_IM as $im) {
			if(key_exists('url',$im)) printf("GownFull.instance.RegisterDownload('%s',\"%s\",\"%s\");\r\n",$im['objname'],$im['displayname'],$im['url']);
			else printf("GownFull.instance.RegisterDownload('%s',\"%s\");\r\n",$im['objname'],$im['displayname']);
		}
		// Auto load assoc word and google suggest
		printf("GownFull.instance.modman.Require('AjaxIMEAssociativeWord');\r\n");
		printf("GownFull.instance.modman.Require('GoogleSuggest');\r\n");
	}
}
else if($type == 'nocreate') {
}
else {
	// Test if frameset
	printf("if(FramesetInstaller.instance.IsFrameset()) {\r\n");
	printf("  FramesetInstaller.code_url = '%s';\r\n",$CFG->GownFull_BASE . $_SERVER["REQUEST_URI"] . '&type=frame');
	printf("  FramesetInstaller.instance.InstallGownFull(FramesetInstaller.instance.SelectLargestAreaFrame());\r\n");
	printf("}\r\n");
	printf("else {\r\n");
	printf("new GownFull(new GenericGownFullBuilder);\r\n");

	
	if(!$_GET['noregdownload']) {
		register_modules();
		foreach($CFG->Available_IM as $im) {
			if(key_exists('url',$im)) printf("GownFull.instance.RegisterDownload('%s',\"%s\",\"%s\");\r\n",$im['objname'],$im['displayname'],$im['url']);
			else printf("GownFull.instance.RegisterDownload('%s',\"%s\");\r\n",$im['objname'],$im['displayname']);
		}
		// Auto load assoc word and google suggest
		printf("GownFull.instance.modman.Require('AjaxIMEAssociativeWord');\r\n");
		printf("GownFull.instance.modman.Require('GoogleSuggest');\r\n");
	}
	printf("}\r\n");
}
?>
